package com.bk.base;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bk.service.impl.CategoryServiceImpl;
import com.bk.service.impl.OrderServiceImpl;
import com.bk.service.impl.ProductServiceImpl;
import com.bk.service.impl.UserServiceImpl;

/**
 * Servlet implementation class BaseServlet
 */
public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	protected UserServiceImpl userServiceImpl=new UserServiceImpl();
	protected CategoryServiceImpl categoryServiceImpl=new CategoryServiceImpl();
	protected ProductServiceImpl productServiceImpl=new ProductServiceImpl();
	protected OrderServiceImpl orderServiceImpl=new OrderServiceImpl();
	public BaseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String method = request.getParameter("action");
		//反射
		//servlet的class文件对象
		Class<? extends BaseServlet> clz = this.getClass();
		//获得方法  方法名  参数
		try {
			Method m = clz.getDeclaredMethod(method,HttpServletRequest.class,HttpServletResponse.class );
			//执行方法invoke(obj, args)  obj：执行该方法的对象    args实际参数
			m.setAccessible(true);//强行可见 暴力破解
			m.invoke(this,request,response );
			
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			
			//TODO
			e.printStackTrace();			
		}	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
